<%--
  Created by IntelliJ IDEA.
  User: W
  Date: 2025/9/27
  Time: 15:41
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>登录</title>
</head>
<body>
<form action="" method="post">
    <div>
        账号：<input type="text" name="username" id="username">
    </div>
    <div>
        密码：<input type="password" name="password" id="password">
    </div>
    <div>
        <input type="button" value="登录" id="loginBtn">
    </div>
</form>
</body>
</html>
<%--<script type="text/javascript" src="js/ajax.js"></script>--%>
<script type="text/javascript" src="js/jquery-3.6.0.js"></script>
<script type="text/javascript">
    $(function (){
        $("#loginBtn").click(function () {
           $.ajax({
               url:'login',
               // method: "post",
               //与普通Ajax的区别这里为type，不是method
               type:'post',
               contentType: "application/x-www-form-urlencoded",
               data:{
                   username:$("#username").val(),
                   password:$("#password").val()
               },
               success:function (resp){
                   if (resp === "1") {
                       alert("登录成功");
                   } else {
                       alert("账号或密码错误");
                   }
               }
           }) ;
        });
    });
    // let loginBut = document.getElementById("loginBtn");
    // loginBut.onclick = function () {
    //     let username = document.getElementById("username").value;
    //     let password = document.getElementById("password").value;
    //     ajax({
    //         url: "login",
    //         method: "post",
    //         contentType: "application/x-www-form-urlencoded",
    //         data: {
    //             username: username,
    //             password: password
    //         },
    //         success:function (resp){
    //             if (resp === "1") {
    //                 alert("登录成功");
    //             } else {
    //                 alert("账号或密码错误");
    //             }
    //         }
    //     })
        /*let xmlHttpRequest;
        if (window.XMLHttpRequest) {
            xmlHttpRequest = new XMLHttpRequest();
        } else {
            // 微软IE
            xmlHttpRequest = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlHttpRequest.onreadystatechange = function () {
            if (xmlHttpRequest.readyState === 4 && xmlHttpRequest.status === 200) {
                //     就绪状态为4，表示数据已经返回
                //     200表示服务器端返回数据成功
                let responseText = xmlHttpRequest.responseText;
                //     对结果进行处理
                if (responseText === "1") {
                    alert("登录成功");
                } else {
                    alert("账号或密码错误");
                }
            }
        }
        xmlHttpRequest.open("post", "login", true);
        //POST方式发送请求时，携带数据一定要添加请求头，设置传输数据的类型
        //为application/x-www-form-urlencoded其作用将键值对的参数用&连接起来，
        //如果有空格，将空格转换为+加号；有特殊符号，将特殊符号转换为ASCII HEX值
        xmlHttpRequest.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xmlHttpRequest.send("username=" + username + "&password=" + password);*/
    // }
</script>
